<template>
  <div id="login">
    <div id="loginBox">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt>
      </div>
      <!-- 登入表单区域 -->
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        label-width="0px"
        class="login_form"
      >
        <!-- 登录 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima "
            placeholder
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登入</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { async } from 'q'
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
    //表单验证规则
  },

  methods: {
    reset() {
      // console.log(this)
      this.$refs.loginForm.resetFields()
    },
    login() {
      this.$refs.loginForm.validate(async flag => {
        // console.log(this.axios)
        if (flag) {
          var { data: res } = await this.axios.post('login', this.loginForm)
          // console.log(res)
          if (res.meta.status == 200) {
            this.$message.success('登入成功')
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          } else {
            return this.$message.error('登入失败')
          }
        }
      })
    }
  },

  created() {}
}
</script>

<style lang='less' scoped>
#login {
  height: 100%;
  background: #333744;
}
#loginBox {
  position: absolute;
  background: #fff;
  width: 400px;
  height: 400px;
  border-radius: 10px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    width: 130px;
    padding: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    position: absolute;
    padding: 10px;
    box-shadow: 0 0 10px rgb(67, 173, 67);
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
.login_form {
  position: absolute;
  width: 100%;
  bottom: 10%;
  padding: 0 20px;
  box-sizing: border-box;
  .input_i {
    border-radius: 10px;
  }
}
</style>
